package com.coffee.admin.service.supplier.impl;

import com.coffee.admin.api.common.service.RedisService;
import com.coffee.admin.api.platform.result.PlatformPermissionResult;
import com.coffee.admin.api.supplier.query.SupplierPermissionQuery;
import com.coffee.admin.api.supplier.result.SupplierPermissionResult;
import com.coffee.admin.api.supplier.service.SupplierPermissionService;
import com.coffee.admin.service.platform.bean.PlatformPermission;
import com.coffee.admin.service.supplier.bean.SupplierPermission;
import com.coffee.admin.service.supplier.dao.SupplierPermissionDao;
import com.coffee.admin.service.supplier.dao.SupplierRoleRelationDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.exception.ValidateBusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class SupplierPermissionServiceImpl implements SupplierPermissionService {
    @Resource
    private SupplierPermissionDao supplierPermissionDao;
    @DubboReference
    private RedisService redisService;
    @Resource
    private SupplierRoleRelationDao supplierRoleRelationDao;


    @Override
    public Page<SupplierPermissionResult> getPager(SupplierPermissionQuery param, PagerInfo pr) {
        Page<SupplierPermissionResult> pager = new Page<SupplierPermissionResult>(pr.getStart(), pr.getPageSize());
        Integer count = supplierPermissionDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<SupplierPermission> list = supplierPermissionDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, SupplierPermissionResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(SupplierPermissionQuery param) {
        Integer count = supplierPermissionDao.count(param);
        return count;
    }

    @Override
    public SupplierPermissionResult getDetail(Integer id) {
        SupplierPermission item = supplierPermissionDao.findById(id);
        if (item == null) {
            return null;
        }
        SupplierPermissionResult result = new SupplierPermissionResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<SupplierPermissionResult> getList(SupplierPermissionQuery param, PagerInfo pr) {
        List<SupplierPermission> list = supplierPermissionDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, SupplierPermissionResult.class);
    }

    @Override
    public List<SupplierPermissionResult> getList(SupplierPermissionQuery param) {
        List<SupplierPermission> list = supplierPermissionDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, SupplierPermissionResult.class);
    }

    @Override
    public List<SupplierPermissionResult> getList() {
        SupplierPermissionQuery param = new SupplierPermissionQuery();
        List<SupplierPermission> list = supplierPermissionDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, SupplierPermissionResult.class);
    }


    @Override
    public void delete(Integer id) {
        supplierPermissionDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(SupplierPermissionResult result) throws BusinessException {
        SupplierPermission item = new SupplierPermission();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            supplierPermissionDao.insert(item);
        } else {
            SupplierPermission tmp = supplierPermissionDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            supplierPermissionDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, SupplierPermissionResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, SupplierPermissionResult> map = new HashMap<Integer, SupplierPermissionResult>();
        List<SupplierPermission> list = supplierPermissionDao.findByIds(StringUtil.setToList(idSet));
        List<SupplierPermissionResult> resultList = DTOUtils.convertList(list, SupplierPermissionResult.class);
        for (SupplierPermissionResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, SupplierPermissionResult> getItemMap() {
        Map<Integer, SupplierPermissionResult> map = new HashMap<Integer, SupplierPermissionResult>();
        SupplierPermissionQuery param = new SupplierPermissionQuery();
        List<SupplierPermission> list = supplierPermissionDao.page(param, 0, Integer.MAX_VALUE);
        List<SupplierPermissionResult> resultList = DTOUtils.convertList(list, SupplierPermissionResult.class);
        for (SupplierPermissionResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public List<SupplierPermissionResult> getPermissionListByLoginMerchant() {
        SupplierPermissionQuery permissionQuery = new SupplierPermissionQuery();
        List<SupplierPermission> supplierPermissions = supplierPermissionDao.list(permissionQuery);

        Map<Integer, List<SupplierPermission>> permissionMap = supplierPermissions.stream().collect(Collectors.groupingBy(SupplierPermission::getPid));

        List<SupplierPermission> permissions = permissionMap.get(0);
        List<SupplierPermissionResult> adminMenuResults = DTOUtils.convertList(permissions, SupplierPermissionResult.class);
        recursion(permissionMap, adminMenuResults);
        return adminMenuResults;
    }


    @Override
    public Set<String> getPermissionSet(Integer platformId) throws BusinessException {
        ValidateBusinessException.assertNonNull(platformId, ExceptionEnum.ERROR_1301);
        Set<String> set = new HashSet<String>();
        List<Integer> roleIdList = supplierRoleRelationDao.findRoleIdListBySupplierId(platformId);
        if (roleIdList == null || roleIdList.size() == 0) {
            return set;
        }
        List<String> localPermissionList = supplierPermissionDao.getSupplierPermissionListByRoleIdList(roleIdList);
        if (localPermissionList == null || localPermissionList.size() == 0) {
            return set;
        } else {
            set.addAll(localPermissionList);
        }
        return set;
    }

    public void recursion(Map<Integer, List<SupplierPermission>> permissionMap, List<SupplierPermissionResult> adminMenuResults) {
        for (SupplierPermissionResult adminMenuResult : adminMenuResults) {
            Integer id = adminMenuResult.getId();
            List<SupplierPermission> adminMenus = permissionMap.get(id);
            if (CollectionUtils.isNotEmpty(adminMenus)) {
                List<SupplierPermissionResult> submenuList = DTOUtils.convertList(adminMenus, SupplierPermissionResult.class);
                recursion(permissionMap, submenuList);
                adminMenuResult.setChildList(submenuList);
            }
        }
    }


}
